/*     */ package com.zimbra.cs.account;
/*     */ 
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.SetUtil;
/*     */ import com.zimbra.cs.account.callback.CallbackContext;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Arrays;
/*     */ import java.util.Collection;
/*     */ import java.util.HashSet;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import java.util.Set;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public abstract class AttributeCallback
/*     */ {
/*     */   public abstract void preModify(CallbackContext paramCallbackContext, String paramString, Object paramObject, Map paramMap, Entry paramEntry)
/*     */     throws ServiceException;
/*     */   
/*     */   public abstract void postModify(CallbackContext paramCallbackContext, String paramString, Entry paramEntry);
/*     */   
/*     */   protected static class SingleValueMod
/*     */   {
/*     */     Mod mMod;
/*     */     String mValue;
/*     */     
/*     */     static enum Mod
/*     */     {
/*  77 */       SETTING, 
/*  78 */       UNSETTING;
/*     */       
/*     */ 
/*     */       private Mod() {}
/*     */     }
/*     */     
/*  84 */     public boolean setting() { return this.mMod == Mod.SETTING; }
/*  85 */     public boolean unsetting() { return this.mMod == Mod.UNSETTING; }
/*  86 */     public String value() { return this.mValue; }
/*     */   }
/*     */   
/*     */   protected static class MultiValueMod {
/*     */     Mod mMod;
/*     */     
/*  92 */     static enum Mod { ADDING, 
/*  93 */       REMOVING, 
/*  94 */       REPLACING, 
/*  95 */       DELETING;
/*     */       
/*     */       private Mod() {} }
/*     */     
/*  99 */     List<String> mValues = new ArrayList();
/*     */     
/* 101 */     public boolean adding() { return this.mMod == Mod.ADDING; }
/* 102 */     public boolean removing() { return this.mMod == Mod.REMOVING; }
/* 103 */     public boolean replacing() { return this.mMod == Mod.REPLACING; }
/* 104 */     public boolean deleting() { return this.mMod == Mod.DELETING; }
/* 105 */     public List<String> values() { return this.mValues; }
/* 106 */     public Set<String> valuesSet() { return new HashSet(this.mValues); }
/*     */   }
/*     */   
/*     */   protected SingleValueMod singleValueMod(String attrName, Object value) throws ServiceException {
/* 110 */     SingleValueMod svm = new SingleValueMod();
/* 111 */     if (value == null) {
/* 112 */       svm.mMod = AttributeCallback.SingleValueMod.Mod.UNSETTING;
/* 113 */     } else { if (!(value instanceof String)) {
/* 114 */         throw ServiceException.INVALID_REQUEST(attrName + " is a single-valued attribute", null);
/*     */       }
/* 116 */       String s = (String)value;
/* 117 */       if ("".equals(s)) {
/* 118 */         svm.mMod = AttributeCallback.SingleValueMod.Mod.UNSETTING;
/*     */       } else {
/* 120 */         svm.mMod = AttributeCallback.SingleValueMod.Mod.SETTING;
/* 121 */         svm.mValue = s;
/*     */       }
/*     */     }
/* 124 */     return svm;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   protected SingleValueMod singleValueMod(Map attrsToModify, String attrName)
/*     */     throws ServiceException
/*     */   {
/* 133 */     SingleValueMod svm = new SingleValueMod();
/*     */     
/* 135 */     Object v = attrsToModify.get("-" + attrName);
/* 136 */     if (v != null) {
/* 137 */       svm.mMod = AttributeCallback.SingleValueMod.Mod.UNSETTING;
/* 138 */       return svm;
/*     */     }
/*     */     
/* 141 */     Object value = attrsToModify.get(attrName);
/* 142 */     if (value == null) {
/* 143 */       value = attrsToModify.get("+" + attrName);
/*     */     }
/* 145 */     if (value == null) {
/* 146 */       svm.mMod = AttributeCallback.SingleValueMod.Mod.UNSETTING;
/*     */     } else {
/* 148 */       String s = null;
/*     */       
/* 150 */       if ((value instanceof String)) {
/* 151 */         s = (String)value;
/* 152 */       } else if ((value instanceof String[])) {
/* 153 */         String[] ss = (String[])value;
/* 154 */         if (ss.length == 1) {
/* 155 */           s = ss[0];
/*     */         }
/*     */       }
/*     */       
/* 159 */       if (s == null) {
/* 160 */         throw ServiceException.INVALID_REQUEST(attrName + " is a single-valued attribute", null);
/*     */       }
/* 162 */       if ("".equals(s)) {
/* 163 */         svm.mMod = AttributeCallback.SingleValueMod.Mod.UNSETTING;
/*     */       } else {
/* 165 */         svm.mMod = AttributeCallback.SingleValueMod.Mod.SETTING;
/* 166 */         svm.mValue = s;
/*     */       }
/*     */     }
/*     */     
/* 170 */     return svm;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   protected MultiValueMod multiValueMod(Map attrsToModify, String attrName)
/*     */     throws ServiceException
/*     */   {
/* 187 */     MultiValueMod mvm = new MultiValueMod();
/* 188 */     Object v = attrsToModify.get(attrName);
/* 189 */     if (v != null) {
/* 190 */       if (((v instanceof String)) && (((String)v).length() == 0)) {
/* 191 */         mvm.mMod = AttributeCallback.MultiValueMod.Mod.DELETING;
/*     */       } else
/* 193 */         mvm.mMod = AttributeCallback.MultiValueMod.Mod.REPLACING;
/* 194 */     } else if (attrsToModify.keySet().contains(attrName))
/*     */     {
/* 196 */       mvm.mMod = AttributeCallback.MultiValueMod.Mod.DELETING;
/*     */     }
/*     */     
/* 199 */     if (v == null) {
/* 200 */       v = attrsToModify.get("+" + attrName);
/* 201 */       if (v != null) {
/* 202 */         mvm.mMod = AttributeCallback.MultiValueMod.Mod.ADDING;
/*     */       }
/*     */     }
/* 205 */     if (v == null) {
/* 206 */       v = attrsToModify.get("-" + attrName);
/* 207 */       if (v != null) {
/* 208 */         mvm.mMod = AttributeCallback.MultiValueMod.Mod.REMOVING;
/*     */       }
/*     */     }
/* 211 */     if ((mvm.mMod != null) && (mvm.mMod != AttributeCallback.MultiValueMod.Mod.DELETING)) {
/* 212 */       mvm.mValues = getMultiValue(v);
/*     */     }
/* 214 */     return mvm.mMod == null ? null : mvm;
/*     */   }
/*     */   
/*     */   protected List<String> getMultiValue(Object value) throws ServiceException
/*     */   {
/* 219 */     List<String> list = null;
/*     */     
/* 221 */     if ((value instanceof String)) {
/* 222 */       list = new ArrayList(1);
/* 223 */       list.add((String)value);
/* 224 */     } else if ((value instanceof String[])) {
/* 225 */       list = new ArrayList(Arrays.asList((String[])value));
/* 226 */     } else if ((value instanceof Collection)) {
/* 227 */       list = new ArrayList();
/* 228 */       for (Object o : (Collection)value)
/* 229 */         list.add(o.toString());
/*     */     } else {
/* 231 */       throw ServiceException.INVALID_REQUEST("value not a String or String[]", null);
/*     */     }
/* 233 */     return list;
/*     */   }
/*     */   
/*     */   protected Set<String> getMultiValueSet(Object value) throws ServiceException
/*     */   {
/* 238 */     Set<String> values = new HashSet();
/*     */     
/* 240 */     if ((value instanceof String)) {
/* 241 */       values.add((String)value);
/* 242 */     } else if ((value instanceof String[])) {
/* 243 */       for (String s : (String[])value) {
/* 244 */         values.add(s);
/*     */       }
/* 246 */     } else if ((value instanceof Collection)) {
/* 247 */       for (Object o : (Collection)value) {
/* 248 */         values.add(o.toString());
/*     */       }
/*     */     } else {
/* 251 */       throw ServiceException.INVALID_REQUEST("value not a String or String[] or a Collection", null);
/*     */     }
/*     */     
/* 254 */     return values;
/*     */   }
/*     */   
/*     */   protected Set<String> newValuesToBe(MultiValueMod mod, Entry entry, String attrName) {
/* 258 */     Set<String> newValues = null;
/* 259 */     if (entry != null) {
/* 260 */       Set<String> curValues = entry.getMultiAttrSet(attrName);
/*     */       
/* 262 */       if (mod == null) {
/* 263 */         newValues = curValues;
/*     */       }
/* 265 */       else if (mod.adding()) {
/* 266 */         newValues = new HashSet();
/* 267 */         SetUtil.union(newValues, curValues, mod.valuesSet());
/* 268 */       } else if (mod.removing()) {
/* 269 */         newValues = SetUtil.subtract(curValues, mod.valuesSet());
/* 270 */       } else if (mod.deleting()) {
/* 271 */         newValues = new HashSet();
/*     */       } else {
/* 273 */         newValues = mod.valuesSet();
/*     */       }
/*     */       
/*     */     }
/* 277 */     else if (mod == null) {
/* 278 */       newValues = new HashSet();
/*     */     } else {
/* 280 */       newValues = mod.valuesSet();
/*     */     }
/*     */     
/* 283 */     return newValues;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/account/AttributeCallback.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */